<template>
  <div class="pure-page-container">
    <div v-if="loading" class="loading-container">
      <i class="el-icon-loading"></i>
      <p>正在加载页面...</p>
    </div>

    <div v-else-if="error" class="error-container">
      <i class="el-icon-warning"></i>
      <p>{{ error }}</p>
    </div>

    <div v-else class="form-content">
      <v-form-render
        ref="vFormRender"
        :form-json="formJson"
        :form-data="formData"
        :option-data="optionData"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: "PurePageView",
  data() {
    return {
      loading: true,
      error: null,
      formJson: null,
      formData: {},
      optionData: {},
    };
  },
  created() {
    this.loadPageData();
  },
  methods: {
    // 加载页面数据
    loadPageData() {
      try {
        // 从URL参数获取页面ID
        const pageId = this.$route.params.pageId;

        if (!pageId) {
          this.error = "未找到页面ID参数";
          this.loading = false;
          return;
        }

        // 从localStorage获取页面列表
        const pagesJson = localStorage.getItem("vform-generated-pages");
        if (!pagesJson) {
          this.error = "未找到页面数据";
          this.loading = false;
          return;
        }

        const pages = JSON.parse(pagesJson);
        const page = pages.find((p) => p.id === pageId);

        if (!page) {
          this.error = `未找到ID为${pageId}的页面`;
          this.loading = false;
          return;
        }

        // 设置页面标题
        if (page.pageName) {
          document.title = page.pageName;
        }

        // 设置表单JSON
        this.formJson = page.formJson;

        if (!this.formJson || !this.formJson.widgetList) {
          this.error = "页面数据格式错误";
          this.loading = false;
          return;
        }

        this.loading = false;
      } catch (error) {
        console.error("加载页面数据失败:", error);
        this.error = "加载页面数据失败：" + error.message;
        this.loading = false;
      }
    },
  },
};
</script>

<style scoped>
.pure-page-container {
  min-height: 100vh;
  background: #ffffff;
  padding: 20px;
}

.loading-container,
.error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  text-align: center;
}

.loading-container i {
  font-size: 48px;
  color: #409eff;
  margin-bottom: 20px;
  animation: rotate 2s linear infinite;
}

.error-container i {
  font-size: 48px;
  color: #f56c6c;
  margin-bottom: 20px;
}

.loading-container p,
.error-container p {
  font-size: 18px;
  color: #606266;
  margin: 0;
}

.form-content {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px 0;
}

/* 旋转动画 */
@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 表单样式优化 */
.form-content >>> .el-form {
  background: transparent;
}

.form-content >>> .el-form-item {
  margin-bottom: 22px;
}

.form-content >>> .el-form-item__label {
  font-weight: 500;
  color: #303133;
  line-height: 1.5;
}

.form-content >>> .el-input__inner,
.form-content >>> .el-textarea__inner,
.form-content >>> .el-select .el-input__inner {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.form-content >>> .el-input__inner:focus,
.form-content >>> .el-textarea__inner:focus,
.form-content >>> .el-select .el-input__inner:focus {
  border-color: #409eff;
}

.form-content >>> .el-button {
  border-radius: 4px;
  font-weight: 500;
}

.form-content >>> .el-button--primary {
  background-color: #409eff;
  border-color: #409eff;
}

.form-content >>> .el-button--primary:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .pure-page-container {
    padding: 15px;
  }

  .form-content {
    padding: 15px 0;
  }

  .loading-container i,
  .error-container i {
    font-size: 36px;
  }

  .loading-container p,
  .error-container p {
    font-size: 16px;
  }
}

@media (max-width: 480px) {
  .pure-page-container {
    padding: 10px;
  }

  .form-content {
    padding: 10px 0;
  }
}
</style>
